/**	Proiectul 1 Inginerie Software
 * 	
 * 	Anul 2 Informatica Romana
 *  
 *  @author Sabau Ciprian
 *  @author Popescu Valentin
 *  @version 1.1 08 Martie 2010
 * 
 */


import q10.Arbori.*;
import q10.Functii.FunctiiElementare.*;
import q10.Functii.FunctiiTrigonometrice.*;
import q10.Operatori.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Proiect1IS extends Panel implements ActionListener {

	Label fxLayout,fxDerLayout,x0Layout;
	TextField fxField,fxDerField,x0Field;
	Button fxLoad,x0Load;
	static String fxInput,fxOutput;
	static double x0;
	String semn;
	
	Vector<String> out=new Vector<String>();
	
	String[] interm=null;//vector intermediar
	/**
	 * @param args
	 */
	
	public void afiseazaVector(Vector<String> v)
		{
		for (int x=0;x<v.size();x++)
			System.out.println(v.get(x)+ " ");
		}
	
	public void parseazaExp(String exp) 
		{
		
		String[] ch=new String[exp.length()];
		for (int k=0;k<exp.length();k++)
			{
			ch[k]=Character.toString(exp.charAt(k));
			}//vector de stringuri cu fiecare caracter din expresie (daca exp=a+b => ch[0]=a,ch[1]=+,ch[2]=b)
		interm=null;
		for (int i=0;i<ch.length;i++)
			{
			
			if (ch[i].equals("+"))
				{
				Nod plus = new Plus();
				semn="plus";
				interm=exp.split("\\+");
				}
			else if(ch[i].equals("-"))
				{
				Nod minus=new Minus();
				semn="minus";
				interm=exp.split("\\-");
				}
			else if(ch[i].equals("\\*"))
				{
				Nod produs = new Produs();
				semn="produs";
				interm=exp.split("*");
				}
			else if(ch[i].equals("/"))
				{
				Nod cat=new Cat();
				semn="cat";
				interm=exp.split("\\/");
				}
			}
		
		for(int j=0;j<interm.length;j++)
			{
			out.add(interm[j]);
			System.out.println(interm[j]+ " | ");	
			}
		afiseazaVector(out);
		

		}
	
	public void InitializareFer()
		{
		setLayout(null);
		
	    fxLayout = new Label(" f(x): ");
	    add(fxLayout);
	    fxLayout.setBounds(0,0,30,30);
	    
	    fxField = new TextField(50);
	    add(fxField);
	    fxField.setBounds(30,0,370,30);
	    
	    fxLoad = new Button("Load");
		add(fxLoad);
		fxLoad.setBounds(400,0,100,30);
		fxLoad.addActionListener(this);
		
	    fxDerLayout = new Label(" f'(x): ");
	    add(fxDerLayout);
	    fxDerLayout.setBounds(0,30,30,30);
	    
	    fxDerField = new TextField(50);
	    add(fxDerField);
	    fxDerField.setBounds(30,30,370,30);
	    fxDerField.setEditable(false);
	    
	    x0Layout = new Label(" x(0): ");
	    add(x0Layout);
	    x0Layout.setBounds(0,60,30,30);
	    
	    x0Field = new TextField(10);
	    add(x0Field);
	    x0Field.setBounds(30,60,370,30);
	    
	    x0Load = new Button("Load");
		add(x0Load);
		x0Load.setBounds(400,60,100,30);
		x0Load.addActionListener(this);
		}

	public void actionPerformed(ActionEvent a)
		{
		if (a.getSource() == fxLoad)
			{
			fxInput=fxField.getText();
			parseazaExp(fxInput);
			}
		fxDerField.setText(fxInput);//de test
		
		if (a.getSource() == x0Load)
			{
			x0=Double.parseDouble(x0Field.getText());
			}
		}
	
	public static void main(String[] args) {
		/*
		Nod n = new Necunoscuta();
	    Nod sin=new Sin();
	    Nod cos=new Cos();
	    Nod xn=new Necunoscuta();
	    Nod cat=new Cat();
	      
	    cat.setfStang(n);
	    cat.setfDrept(sin);
	    sin.setfStang(cos);
	    cos.setfStang(xn);
	    */
	    Nod n1 = new Necunoscuta();
	    Nod sin1 = new Sin();
	    Nod ln1 = new Ln();
	    Nod exp1 = new Exp();
	    Nod prod1 = new Produs();
	    Nod suma1 = new Plus();
	    Nod putere1=new Putere();
	    
	    ln1.setfStang(n1); 
	    sin1.setfStang(ln1);
	    
	    putere1.setfDrept(n1);
	    putere1.setfStang(n1);
	    
	    exp1.setfStang(n1);
	    prod1.setfDrept(n1);
	    prod1.setfStang(exp1);
	    
	    suma1.setfDrept(sin1);
	    suma1.setfStang(prod1);
	    
//	    System.out.println(putere1.derivare());
		
//	    ArboriFunctionali a = new ArboriFunctionali(cat);
//	    ArboriFunctionali b = new ArboriFunctionali(suma1);
//	    System.out.println(b);
//	    System.out.println(b.derivata());
//	    System.out.println(fxInput);
	    
//	    System.out.println(b.derivare());
//	    System.out.println(a);  
//	    System.out.println(a.derivare());
//	    System.out.println(x0);
//	    fxOutput=a.derivare();
	    
	    //Interfata Grafica

	    Frame fer = new Frame("Test");
	    Proiect1IS p=new Proiect1IS();
	    p.InitializareFer();
	    fer.addWindowListener(new WindowAdapter() {
	        public void windowClosing(WindowEvent evt) {
	            Frame fr = (Frame)evt.getSource();
	            fr.setVisible(false);
	           fr.dispose();
	        }});
	    fer.add(p);
		fer.setSize(500,500);
		fer.setVisible(true);

	}
	
}
